/*
	   _           _      _           _     _ 
	  (_)         | |    | |         (_)   | |
	   _ _   _ ___| |_ __| |_ __ ___  _  __| |
	  | | | | / __| __/ _` | '__/ _ \| |/ _` |
	  | | |_| \__ \ || (_| | | | (_) | | (_| |
	  | |\__,_|___/\__\__,_|_|  \___/|_|\__,_|
	 _/ |                                     
	|__/ 
	
	Copyright 2013 Caspar Isemer and and Eva Krueger, http://justdroid.org
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
		http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License. 
*/

package org.justdroid.justdroid;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.Vibrator;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.Data;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ContactUpdate extends JustDroidActivity {
	
	/** Declare variables and UI elements **/
	private EditText editField, editEmail;
	private TextView txtType;
	private Button btnSave, btnSetPrimary;//, btnDelete;
	private TextWatcher textWatcher;
	private Spinner spinnerType;
	private SpinnerAdapter adapter;
	private String id, name, number, numType, email;
	private int typeIndex;
	private int editFlag, currentSelection = -1 ;
	private final int NAME = 1;
	private final int NUMBER = 2;
	private final int EMAIL = 3;
	
	void attachKeyListener(final Button button, final int buttonFlag) {
		button.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
				if(keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
					switch(keyCode) {
					case KeyEvent.KEYCODE_DPAD_CENTER:
					case KeyEvent.KEYCODE_ENTER:
						switch(buttonFlag) {
							case 1:
								setPrimaryNumberAndGiveFeedback();
								break;
							case 2:
								setPrimaryMailAndGiveFeedback();
								break;
						}
						break;
					}
				}
				return false;
			}
		});
	}
	
	void setPrimaryMailAndGiveFeedback() {
		if(setPrimaryMail(ContactUpdate.this.id, ContactUpdate.this.email)) {
			//give feedback
			if(Utils.isAccessibilityEnabled(getApplicationContext()))
				TTS.speak(getResources().getString(R.string.setprimarymailsuccess));
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.setprimarymailsuccess), Toast.LENGTH_SHORT).show();
			btnSetPrimary.setVisibility(View.GONE);
		}
		else {
			//give feedback
			if(Utils.isAccessibilityEnabled(getApplicationContext()))
				TTS.speak(getResources().getString(R.string.setprimarymailfailed));
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.setprimarymailfailed), Toast.LENGTH_SHORT).show();								
		}
	}
	
	void setPrimaryNumberAndGiveFeedback() {
		if(setPrimaryNumber(ContactUpdate.this.id, ContactUpdate.this.number)) {
			//give feedback
			if(Utils.isAccessibilityEnabled(getApplicationContext()))
				TTS.speak(getResources().getString(R.string.setprimarynumbersuccess));
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.setprimarynumbersuccess), Toast.LENGTH_SHORT).show();
			btnSetPrimary.setVisibility(View.GONE);
		}
		else {
			//give feedback
			if(Utils.isAccessibilityEnabled(getApplicationContext()))
				TTS.speak(getResources().getString(R.string.setprimarynumberfailed));
			Toast.makeText(getApplicationContext(), getResources().getString(R.string.setprimarynumberfailed), Toast.LENGTH_SHORT).show();								
		}
	}
	
	/** Function to check if the number is set as the primary number **/
	boolean isPrimaryNumber(String contactId, String contactNumber) {
		Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, new String[] {Phone.DATA, Phone.IS_PRIMARY}, Phone.CONTACT_ID + "=?", new String[] { contactId }, null);
		int index = cursor.getColumnIndex(Phone.DATA);
		while (cursor.moveToNext()) {
				String phoneNumber = cursor.getString(index);
				if(phoneNumber.equals(contactNumber)) {
					if(!(cursor.getString(cursor.getColumnIndex(Phone.IS_PRIMARY)).equals("0"))) {
						//number is primary
						cursor.close();
						return true;
					}
				}
		}
		cursor.close();
		return false;
	}
	
	/** function to check if the email id is set as the primary email id **/
	@SuppressLint("InlinedApi")
	boolean isPrimaryMail(String contactId, String emailId) {
		Cursor cursor = getContentResolver().query( 
            ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
            null,
            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
            new String[]{contactId}, null);
		
        while (cursor.moveToNext()) { 
        	String mail = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));    //TODO: change to ADDRESS                         
        	Log.e(mail, cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.IS_PRIMARY)));
        	
        	if(emailId.equals(mail)) {
        		if(!(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.IS_PRIMARY)).equals("0"))) {
					//email id is primary 
					return true;
				}
        	}
        } 
        cursor.close();
        return false;
	}
	/** For Version 2 
	/** Function to delete number/email **/
	
	/** Function to set a number as the primary number for the contact **/
	boolean setPrimaryNumber(String contactId, String contactNumber) {
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
		@SuppressWarnings("unused")
		Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { contactId }, null);
		String where = ContactsContract.Data.CONTACT_ID + " = ? AND " +
			        ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?";
		
			        String[] params = new String[] {contactId,
			                contactNumber};

			         ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
			                .withSelection(where, params)
			                .withValue(ContactsContract.CommonDataKinds.Phone.IS_PRIMARY, 1)
			                .build());


			         try {
			             	getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
			             return true;    
			            } catch (RemoteException e) {
			                e.printStackTrace();
			                return false;
			            } catch (OperationApplicationException e) {
			                e.printStackTrace();
			                return false;
			            }

	}
	
	/** Function to set an email id as the primary email id for the contact **/
	@SuppressLint("InlinedApi")
	boolean setPrimaryMail(String contactId, String emailId) {
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
		@SuppressWarnings("unused")
		Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
				ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=?", new String[] { contactId }, null);
		String where = ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ? AND " +
			        ContactsContract.CommonDataKinds.Email.ADDRESS + " = ?"; //TODO: change to address

			        String[] params = new String[] {contactId, emailId};

			         ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
			                .withSelection(where, params)
			                .withValue(ContactsContract.CommonDataKinds.Email.IS_PRIMARY, 1)
			                .build());


			         try {
			             	getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
			             return true;    
			            } catch (RemoteException e) {
			                e.printStackTrace();
			                return false;
			            } catch (OperationApplicationException e) {
			                e.printStackTrace();
			                return false;
			            }

	}

	/** Function to attach onTouchListener and onFocusListener to the EditTexts **/
	void attachOnTouchAndFocusListener(final EditText editText) {
		final String text = editText.getText().toString().trim();
		/*editText.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				//vibrate
				Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
			    vibrator.vibrate(500);
				if(text.equals("")) {
					//empty EditText
					TTS.speak(editText.getContentDescription().toString());
				}
				else {
					TTS.speak(editText.getText().toString());
				}
				return false;
			}
		});*/
		
		editText.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View view, boolean hasFocus) {
				if(hasFocus) {
					if(text.equals("")) {
						//empty EditText
						TTS.speak(editText.getContentDescription().toString());
					}
					else {
						TTS.speak(editText.getText().toString());
					}
				}
			}
		});
	}
	
	/** Function to announce the text of the view that received focus or on which the onTouch event was triggered**/
	void giveFeedback(String text) {
		//vibrate
		Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	    vibrator.vibrate(300);
	    //TTS feedback
	    TTS.speak(text);
	}
	
	/** Function to save the new contact **/
	boolean saveContact() {
		ArrayList<ContentProviderOperation> op_list = new ArrayList<ContentProviderOperation>(); 
	    op_list.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI) 
	            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 
	            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
	            .build()); 

	    // first and last names 
	    op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
	            .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
	            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) 
	            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, this.editField.getText().toString())
	            .build()); 

	    op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
	            .withValueBackReference(Data.RAW_CONTACT_ID, 0) 
	            .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
	            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, this.number)
	            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, this.typeIndex)
	            .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, this.spinnerType.getSelectedItem().toString())
	            .build());
	    op_list.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 
	            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
	            .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
	            .withValue(ContactsContract.CommonDataKinds.Email.DATA, editEmail.getText().toString())
	            .build());

	    try{ 
	        @SuppressWarnings("unused")
			ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, op_list); 
	    }catch(Exception e){ 
	        e.printStackTrace();
	       return false;
	    } 
	        return true;
	}
	
	/** Function to edit a contact **/
	@SuppressLint("InlinedApi")
	boolean editContact(String contactId, int flag) {
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
		switch(flag) {
		case NAME:
			Log.e("this", contactId+" " +this.editField.getText().toString());
			ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
	        		.withSelection(Data.CONTACT_ID + "=? AND " + Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'", new String[]{contactId})
	        		.withValue(StructuredName.DISPLAY_NAME, this.editField.getText().toString())
		            .build());
			break;
		case NUMBER:
			ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
	        		.withSelection(Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE + "'", new String[]{contactId})
	        		.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, this.editField.getText().toString())
		            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, this.typeIndex)
		            .withValue(ContactsContract.CommonDataKinds.Phone.LABEL, this.spinnerType.getSelectedItem().toString())
		            .build());
			break;
		case EMAIL:
			ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI) 
	        		.withSelection(Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE + "'", new String[]{contactId})
	        		.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, this.editField.getText().toString())
		            .build());
			break;
		}
		try { 
			@SuppressWarnings("unused")
			ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
		}catch(Exception e) { 
			e.printStackTrace();
		    return false;
		}
		return true;
	}
	
	/** Create the Contact Details menu activity **/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.contactupdate);
		super.onCreate(savedInstanceState);
		
		/** get UI elements **/
		//txtName = (TextView) findViewById(R.id.txtName);
		editField = (EditText) findViewById(R.id.editField);
		//editEmail = (EditText) findViewById(R.id.editEmail);
		txtType = (TextView) findViewById(R.id.txtType);
		btnSave = (Button) findViewById(R.id.btnUpdate);
		btnSetPrimary = (Button) findViewById(R.id.btnSetPrimary);
		spinnerType = (Spinner) findViewById(R.id.spinnerType);
		
		adapter = new SpinnerAdapter(getApplicationContext(), Utils.numberType);
		spinnerType.setAdapter(adapter);
		
		if(getIntent().getExtras() != null) {
			//id, name, number, numtype, email
			this.id = getIntent().getExtras().getString("id");
			
			if(getIntent().hasExtra("name")) {
				this.editFlag = NAME;
				this.name = getIntent().getExtras().getString("name");
				editField.setText(this.name);
				txtType.setVisibility(View.GONE);
				spinnerType.setVisibility(View.GONE);
				btnSetPrimary.setVisibility(View.GONE);
			}
			if(getIntent().hasExtra("number")) {
				this.editFlag = NUMBER;
				this.number = getIntent().getExtras().getString("number");
				editField.setText(this.number);
				editField.setInputType(InputType.TYPE_CLASS_PHONE);
				this.numType = getIntent().getExtras().getString("numtype");
				txtType.setVisibility(View.VISIBLE);
				spinnerType.setVisibility(View.VISIBLE);
				if(numType.equals((getResources().getStringArray(R.array.type))[0])) //mobile
					spinnerType.setSelection(0);
				else if(numType.equals((getResources().getStringArray(R.array.type))[1])) //home
					spinnerType.setSelection(1);
				else if(numType.equals((getResources().getStringArray(R.array.type))[2])) //work
					spinnerType.setSelection(2);
				else if(numType.equals((getResources().getStringArray(R.array.type))[3])) //work mobile
					spinnerType.setSelection(3);
				else if(numType.equals((getResources().getStringArray(R.array.type))[4])) //home fax
					spinnerType.setSelection(4);
				else if(numType.equals((getResources().getStringArray(R.array.type))[5])) //pager
					spinnerType.setSelection(5);
				else
					spinnerType.setSelection(6); //other
				if(isPrimaryNumber(this.id, this.number)) {
					btnSetPrimary.setVisibility(View.GONE);
				}
				else {
					//if not primary, display set primary button, on click of Set Primary, set delete button's visibility to GONE
					btnSetPrimary.setVisibility(View.VISIBLE);
					
					attachKeyListener(btnSetPrimary, 1);
					
					btnSetPrimary.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View view) {
							setPrimaryNumberAndGiveFeedback();
						}
					});
				}
			}
			if(getIntent().hasExtra("mail")) {
				this.editFlag = EMAIL;
				this.email = getIntent().getExtras().getString("mail");
				editField.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
				editField.setText(this.email);
				txtType.setVisibility(View.GONE);
				spinnerType.setVisibility(View.GONE);
				if(isPrimaryMail(this.id, this.email)) {
					btnSetPrimary.setVisibility(View.GONE);
				}
				else {
					//if not primary, display set primary button, on click of Set Primary, set delete button's visibility to GONE
					btnSetPrimary.setVisibility(View.VISIBLE);
					
					attachKeyListener(btnSetPrimary, 2);
					
					btnSetPrimary.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View view) {
							setPrimaryMailAndGiveFeedback();
						}
					});
				}
			}
		}
		
		
		textWatcher = new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
				if(Utils.isAccessibilityEnabled(getApplicationContext()))
					TTS.speak(TTS.readNumber(cs.toString()));
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {				
			}
		};
		
		editField.addTextChangedListener(textWatcher);
		
		/*txtType.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View view, MotionEvent arg1) {
				giveFeedback(((TextView)view).getText().toString());
				return false;
			}
		});*/
		
		txtType.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View view, boolean hasFocus) {
				if(hasFocus) {
					giveFeedback(((TextView)view).getText().toString());
				}
			}
		});
		
		/*spinnerType.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				//vibrate
				Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
			    vibrator.vibrate(500);
				return false;
			}
		});*/
		
		spinnerType.setOnKeyListener(new OnKeyListener() {
			
			@Override
			public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
				Spinner spinner = (Spinner) view;
				if(keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
					switch(keyCode) {
					case KeyEvent.KEYCODE_DPAD_DOWN:
						currentSelection++;
						if(currentSelection == spinner.getCount()) {
							currentSelection = 0;
						}
						giveFeedback(spinnerType.getItemAtPosition(currentSelection).toString());
						spinner.setSelection(currentSelection);
						break;
					}
				}
				return false;
			}
		});
		
		spinnerType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				//if(!TTS.isSpeaking())
					//TTS.speak(spinnerType.getSelectedItem().toString());
				if(spinnerType.getSelectedItem().toString().equalsIgnoreCase("mobile")) {
					ContactUpdate.this.typeIndex = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
				}
				else if(spinnerType.getSelectedItem().toString().equalsIgnoreCase("home")) {
					ContactUpdate.this.typeIndex = ContactsContract.CommonDataKinds.Phone.TYPE_HOME;
				}
				else if(spinnerType.getSelectedItem().toString().equalsIgnoreCase("work")) {
					ContactUpdate.this.typeIndex = ContactsContract.CommonDataKinds.Phone.TYPE_WORK;
				}
				else if(spinnerType.getSelectedItem().toString().equalsIgnoreCase("workmobile")) {
					ContactUpdate.this.typeIndex = ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE;
				}
				else if(spinnerType.getSelectedItem().toString().equalsIgnoreCase("work")) {
					ContactUpdate.this.typeIndex = ContactsContract.CommonDataKinds.Phone.TYPE_WORK;
				}
				else if(spinnerType.getSelectedItem().toString().equalsIgnoreCase("pager")) {
					ContactUpdate.this.typeIndex = ContactsContract.CommonDataKinds.Phone.TYPE_PAGER;
				}
				else if(spinnerType.getSelectedItem().toString().equalsIgnoreCase("other")) {
					ContactUpdate.this.typeIndex = ContactsContract.CommonDataKinds.Phone.TYPE_OTHER;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		/*btnSave.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View view, MotionEvent arg1) {
				giveFeedback(((Button)view).getText().toString());
				return false;
			}
		});*/
		
		btnSave.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View view, boolean hasFocus) {
				if(hasFocus) {
					giveFeedback(((Button)view).getText().toString());
				}
			}
		});
		
		btnSave.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
					//get Id of contact
					//edit contact
					if(editContact(ContactUpdate.this.id, ContactUpdate.this.editFlag)) {
						Toast.makeText(getApplicationContext(), getResources().getString(R.string.contactupdated), Toast.LENGTH_SHORT).show();
						if(Utils.isAccessibilityEnabled(getApplicationContext()))
							TTS.speak(getResources().getString(R.string.contactupdated));
						finish();
					}
					else {
						Toast.makeText(getApplicationContext(), getResources().getString(R.string.contactnotupdated), Toast.LENGTH_SHORT).show();
						if(Utils.isAccessibilityEnabled(getApplicationContext()))
							TTS.speak(getResources().getString(R.string.contactnotupdated));
					}
			}
		});
		/** Put most everything before here **/
	}
	
	@Override
	public void onResume() {
		super.onResume();
		//TTS.speak(getResources().getString(R.string.editcontact));
		//get the root layout
		LinearLayout layout = (LinearLayout) findViewById(R.id.contactupdate);
		Utils.applyFontColorChanges(getApplicationContext(), layout);  Utils.applyFontSizeChanges(getApplicationContext(), layout);  Utils.applyFontTypeChanges(getApplicationContext(), layout);
	}
}
